﻿using Furion.RemoteRequest;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using net8.Helper;

namespace net8.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class LinqController : ControllerBase
    {
        [HttpGet]
        public void GetList()
        {
            List<Group> groups = new List<Group>()
            {
                new Group(){Id=1,GroupName="A"},
                new Group(){Id=2,GroupName="B"},
                new Group(){Id=3,GroupName="C"},
            };
            List<User> users = new List<User>()
            {
                new User(){Id=1,UserName="张1",GroupId=1},
                new User(){Id=2,UserName="张2",GroupId=2},
                new User(){Id=3,UserName="张3",GroupId=4},
            };

            //内连接
            var a1 = from g in groups
                     join u in users on g.Id equals u.GroupId
                     select new { g.GroupName, u.UserName };
            var a2 = groups.Join(users, g => g.Id, u => u.GroupId, (g, u) => new { g.GroupName, u.UserName });

            //左连接
            var b1 = from g in groups
                     join u in users on g.Id equals u.GroupId into gus
                     from gu in gus.DefaultIfEmpty()
                     select new { g.GroupName, gu?.UserName };
            var b2 = groups.GroupJoin(users, g => g.Id, u => u.GroupId, (g, gu) => new { g, gu })
                .SelectMany(t => t.gu.DefaultIfEmpty(), (t, t1) => new { t.g.GroupName, t1?.UserName });

            b2 = groups.GroupJoin(users, g => g.Id, u => u.GroupId, (g, gu) => new { g.GroupName, gu=gu.DefaultIfEmpty() })
                .Select(g => new {g.GroupName, UserName = g.gu.Select(u=>u?.UserName).First()});

            //右连接
            var c1 = from u in users
                     join g in groups on u.GroupId equals g.Id into gus
                     from gu in gus.DefaultIfEmpty()
                     select new { gu?.GroupName, u.UserName };
            var c2 = users.GroupJoin(groups, u => u.GroupId, g => g.Id, (u, gu) => new { u, gu })
                .SelectMany(t => t.gu.DefaultIfEmpty(), (t, g1) => new { g1?.GroupName, t.u.UserName });

            var d1 = b1.Concat(c1).Distinct();

            var d2 = b2.Concat(c2);
            d2 = groups.GroupJoin(users, g => g.Id, u => u.GroupId, (g, gu) => new { g, gu })
                .SelectMany(t => t.gu.DefaultIfEmpty(), (t, t1) => new { t.g.GroupName, t1?.UserName })
                .Concat(users.GroupJoin(groups, u => u.GroupId, g => g.Id, (u, gu) => new { u, gu })
                .SelectMany(t => t.gu.DefaultIfEmpty(), (t, g1) => new { g1?.GroupName, t.u.UserName }));

            //笛卡儿积(CROSS JOIN)
            var e1 = from g in groups
                     from u in users
                     select new { g.GroupName, u.UserName };
            var e2 = groups.SelectMany(g => users, (g, u) => new { g.GroupName, u.UserName });
        }

        [HttpGet]
        public void GetToken()
        {
            //JwtHelper.Encrypt("123456789123456789123456789123456789", 1);
            //JwtHelper.GetUserByToken();
            var server = AppConfig.ServiceProvider.GetService(typeof(IServer)) as IServer;
            string aaa = server.Features.Get<IServerAddressesFeature>().Addresses.First();
        }
    }

    public class Group
    {
        public int Id { get; set; }
        public string GroupName { get; set; }
    }
    public class User
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public int GroupId { get; set; }
    }
}
